{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import psycopg\n",
    "from age.networkx import *\n",
    "from age import *\n",
    "import networkx as nx\n",
    "\n",
    "conn = psycopg.connect(\n",
    "    host=\"localhost\",\n",
    "    port=\"5432\",\n",
    "    dbname=\"postgres\",\n",
    "    user=\"moontasir\",\n",
    "    password=\"254826\")\n",
    "graphName = 'bitnine_global_inic'\n",
    "\n",
    "\n",
    "age.setUpAge(conn, graphName)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Creating a Random Networkx Graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DiGraph with 5 nodes and 9 edges\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "num_nodes = 5\n",
    "num_edges = 10\n",
    "G = nx.DiGraph()\n",
    "try:\n",
    "    for i in range(num_nodes//2):\n",
    "        G.add_node(i, label='Number',properties={'name' : i})\n",
    "    for i in range(num_nodes//2,num_nodes):\n",
    "        G.add_node(i, label='Integer',properties={'age' : i*2} )\n",
    "    for i in range(num_edges):\n",
    "        source = random.randint(0, num_nodes-1)\n",
    "        target = random.randint(0, num_nodes-1)\n",
    "        G.add_edge(source, target, label='Connection' ,properties={'st' : source , 'ed':target})\n",
    "except Exception as e:\n",
    "    raise e\n",
    "print(G)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Networkx to AGE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "networkx_to_age(conn, G, graphName)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## AGE to Networkx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ALL AGE to Networkx\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DiGraph with 5 nodes and 9 edges\n"
     ]
    }
   ],
   "source": [
    "G = age_to_networkx(conn, graphName)\n",
    "print(G)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add subgraph of AGE to Networkx using query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DiGraph with 5 nodes and 9 edges\n"
     ]
    }
   ],
   "source": [
    "G = age_to_networkx(conn, graphName, \n",
    "                    query=\"\"\"\n",
    "SELECT * from cypher('%s', $$\n",
    "        MATCH (V:Integer)\n",
    "        RETURN V\n",
    "$$) as (V agtype);\n",
    "\"\"\" % graphName)\n",
    "\n",
    "G = age_to_networkx(conn, graphName, G=G,\n",
    "                    query=\"\"\"\n",
    "SELECT * from cypher('%s', $$\n",
    "        MATCH (V:Number)\n",
    "        RETURN V\n",
    "$$) as (V agtype);\n",
    "\"\"\" % graphName)\n",
    "\n",
    "G = age_to_networkx(conn, graphName, G=G,\n",
    "                    query=\"\"\"\n",
    "SELECT * from cypher('%s', $$\n",
    "        MATCH (V)-[R]->(V2)\n",
    "        RETURN V,R,V2\n",
    "$$) as (V agtype, R agtype, V2 agtype);\n",
    "\"\"\" % graphName)\n",
    "print(G)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "myenv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
